package com.example.suanfademo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSumHash {
    /**
     * 一个数组，一个target，返回数组中两个值为target的下标
     * @param a
     * @param b
     * @return
     */
    private int[] twoSumHash(int[] a, int b){
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0; i<a.length; i++){
            if(map.containsKey(b-a[i])){
                return new int[]{map.get(b-a[i]),i};
            }
            map.put(a[i],i);
        }
        return new int[]{-1,-1};
    }

    public static void main(String[] args) {
        TwoSumHash twoSumHash = new TwoSumHash();
        int[] ints = twoSumHash.twoSumHash(new int[]{2, 5, 111, 1111}, 7);
        System.out.println(Arrays.toString(ints));
    }
}
